﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
/// <summary>
	/// 数据访问类:Users
	/// </summary>
	public partial class UsersService
	{
		#region  Method

        public static bool UserLogin(string username,string pwd,bool isTeacher)
        {
            string sql = string.Format("SELECT * FROM Users WHERE username = '{0}' AND pwd = '{1}' AND isTeacher = '{2}'",username,pwd,isTeacher);
            SqlDataReader dr = SQLHelper.ExecuteReader(SQLHelper.sqlConnectionString, CommandType.Text, sql);
            if (dr.HasRows)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static Model.Users SelectByID(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM Users ");
            strSql.AppendFormat(" WHERE id = {0}", id);
            SqlDataReader dr = DbHelperSQL.ExecuteReader(strSql.ToString());
            Model.Users u = new Model.Users();
            dr.Read();
            u.id = int.Parse(dr[0].ToString());
            u.sid = dr[1].ToString();
            u.name = dr[2].ToString();
            u.username = dr[3].ToString();
            u.pwd = dr[4].ToString();
            dr.Close();
            return u;
        }
        public static Model.Users SelectByUsername(string username)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM Users ");
            strSql.AppendFormat(" WHERE username = '{0}'",username);
            SqlDataReader dr = DbHelperSQL.ExecuteReader(strSql.ToString());
            Model.Users u = new Model.Users();
            dr.Read();
            u.id = int.Parse(dr[0].ToString());
            u.sid = dr[1].ToString();
            u.name = dr[2].ToString();
            u.username = dr[3].ToString();
            u.pwd = dr[4].ToString();
            dr.Close();
            return u;
        }

		/// <summary>
		/// 增加一条数据
		/// </summary>
        public static int Add(Model.Users model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Users(");
			strSql.Append("sid,name,username,pwd,isTeacher)");
			strSql.Append(" values (");
			strSql.Append("@sid,@name,@username,@pwd,@isTeacher)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@sid", SqlDbType.VarChar,32),
					new SqlParameter("@name", SqlDbType.VarChar,50),
					new SqlParameter("@username", SqlDbType.VarChar,32),
					new SqlParameter("@pwd", SqlDbType.VarChar,32),
					new SqlParameter("@isTeacher", SqlDbType.Bit,1)};
			parameters[0].Value = model.sid;
			parameters[1].Value = model.name;
			parameters[2].Value = model.username;
			parameters[3].Value = model.pwd;
			parameters[4].Value = model.isTeacher;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
        public static bool Update(Model.Users model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Users set ");
			strSql.Append("sid=@sid,");
			strSql.Append("name=@name,");
			strSql.Append("username=@username,");
			strSql.Append("pwd=@pwd,");
			strSql.Append("isTeacher=@isTeacher");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@sid", SqlDbType.VarChar,32),
					new SqlParameter("@name", SqlDbType.VarChar,50),
					new SqlParameter("@username", SqlDbType.VarChar,32),
					new SqlParameter("@pwd", SqlDbType.VarChar,32),
					new SqlParameter("@isTeacher", SqlDbType.Bit,1),
					new SqlParameter("@id", SqlDbType.Int,4)};
			parameters[0].Value = model.sid;
			parameters[1].Value = model.name;
			parameters[2].Value = model.username;
			parameters[3].Value = model.pwd;
			parameters[4].Value = model.isTeacher;
			parameters[5].Value = model.id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
        public static void Delete(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Users ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public static DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,sid,name,username,pwd,isTeacher ");
			strSql.Append(" FROM Users ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}
        #endregion  Method
    }
}
